//
//  BSMyCollectionViewController.m
//  BS
//
//  Created by 王浩宇 on 15/8/14.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "BSMyCollectionViewController.h"
#import "BSMyCollectionViewCell.h"
#import "Header.h"
#import "BSHearderCollectionReusableView.h"
#import "BSDetailsViewController.h"
#import "BSloginAndRegisterViewController.h"
#import "SDImageCache.h"
#import "BSSettingViewController.h"
#import "PKRequestManager.h"
#import "BSActivityViewController.h"

@interface BSMyCollectionViewController ()
@property (nonatomic, strong)NSMutableArray *listArr;

@end

@implementation BSMyCollectionViewController

//static NSString * const reuseIdentifier = @"Cell";
- (NSMutableArray *)listArr
{
    if (_listArr == nil) {
        _listArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _listArr;
}

- (void)fildData
{
    [PKRequestManager manager:GET requestWith:@"http://api.budejie.com/api/api_open.php?a=square&appname=baisishequ&asid=9A2EA4AE-2DEB-4479-A977-8CB5B3EFF60F&c=topic&client=iphone&device=ios%20%E8%AE%BE%E5%A4%87&jbk=0&mac=&market=&openudid=f60e4fa5905641dfd3bec86efbf33da57105acc6&sex=m&udid=&uid=15567628&ver=3.6.1" parDic:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *arr = dic[@"square_list"];
        for (NSDictionary *listDic in arr) {
            BSMysquareModel *model = [[BSMysquareModel alloc] init];
            [model setValuesForKeysWithDictionary:listDic];
            [self.listArr addObject:model];
        }
        [self.collectionView reloadData];

    } error:^(NSError *error) {
        NSLog(@"");
    }];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self fildData];
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(doChangeCloro) name:@"夜间模式" object:nil];
    
    UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 60, 45)];
    UIBarButtonItem *naghtButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"mine-moon-icon"] style:UIBarButtonItemStylePlain target:self action:@selector(nightmode)];
    
    UIBarButtonItem *settingButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"mine-setting-iconN"] style:UIBarButtonItemStylePlain target:self action:@selector(doSteting)];
    tools.items = @[naghtButton,settingButton];
    UIBarButtonItem *coustButton = [[UIBarButtonItem alloc] initWithCustomView:tools];
    self.navigationItem.rightBarButtonItem = coustButton;
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH / 4 - 1, SCREEN_WIDTH / 4 + 20);
    flowLayout.minimumInteritemSpacing = 0.01;
    flowLayout.minimumLineSpacing = 1;
    flowLayout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 120);
    self.collectionView.collectionViewLayout = flowLayout;
    

    [self.collectionView registerNib:[UINib nibWithNibName:@"BSMyCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
    [self.collectionView registerNib:[UINib nibWithNibName:@"BSHearderCollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"hearder"];
    
    
    // Do any additional setup after loading the view.
}

+(float)folderSizeAtPath:(NSString *)path{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    float folderSize;
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles=[fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            NSString *absolutePath=[path stringByAppendingPathComponent:fileName];
            //            folderSize += [FileService fileSizeAtPath:absolutePath];
        }
        folderSize+=[[SDImageCache sharedImageCache] getSize]/1024.0/1024.0;
        return folderSize;
    }
    return 0;
}


- (void)doChangeCloro
{
    self.view.backgroundColor = [UIColor redColor];
}
- (void)nightmode
{
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    NSNotification *notification = [[NSNotification alloc] initWithName:@"夜间模式" object:nil userInfo:nil];
    [center postNotification:notification];
}
- (void)doSteting
{
    BSSettingViewController *settingVC = [[BSSettingViewController alloc] init];
    [self.navigationController pushViewController:settingVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0.01, 0, 0, 0);
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _listArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BSMyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.mySquareModel = _listArr[indexPath.item];
    
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
    if (kind == UICollectionElementKindSectionHeader) {
        BSHearderCollectionReusableView *hearder = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"hearder" forIndexPath:indexPath];
        [hearder.iconImage addTarget:self action:@selector(doLoginView:) forControlEvents:UIControlEventTouchUpInside];
        return hearder;
    }else{
        return nil;
    }
}

- (void)doLoginView:(UIButton *)button
{
    BSloginAndRegisterViewController *loginVC = [[BSloginAndRegisterViewController alloc] init];
    [self presentViewController:loginVC animated:YES completion:^{
        
    }];
    
    
}

#pragma mark <UICollectionViewDelegate>

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 1) {
        BSActivityViewController *activityVC = [[BSActivityViewController alloc] init];
        [self.navigationController pushViewController:activityVC animated:YES];
    }else{
    BSDetailsViewController *detailsVC = [[BSDetailsViewController alloc] init];
    BSMysquareModel *model = _listArr[indexPath.row];
    detailsVC.webUrl = model.url;
    [self.navigationController pushViewController:detailsVC animated:YES];
    }
    
}

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
